home *** CD-ROM | disk | FTP | other *** search
/ Openstep 4.2 (Developer) / Openstep Developer 4.2.iso / NextDeveloper / Source / GNU / uucp / Uucp.framework / unix.subproj / tcp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-09  |  10.7 KB  |  445 lines

  1. /* tcp.c
  2.    Code to handle TCP connections.
  3.  
  4.    Copyright (C) 1991, 1992, 1993, 1995 Ian Lance Taylor
  5.  
  6.    This file is part of the Taylor UUCP package.
  7.  
  8.    This program is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU General Public License as
  10.    published by the Free Software Foundation; either version 2 of the
  11.    License, or (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
  24.    */
  25.  
  26. #include "uucp.h"
  27.  
  28. #if USE_RCS_ID
  29. const char tcp_rcsid[] = "$Id: tcp.c,v 1.5 1995/06/21 19:20:46 ian Rel $";
  30. #endif
  31.  
  32. #if HAVE_TCP
  33.  
  34. #include "uudefs.h"
  35. #include "uuconf.h"
  36. #include "sysdep.h"
  37. #include "conn.h"
  38. #include "system.h"
  39.  
  40. #include <errno.h>
  41.  
  42. #if HAVE_SYS_TYPES_TCP_H
  43. #include <sys/types.tcp.h>
  44. #endif
  45. #include <sys/socket.h>
  46. #include <netdb.h>
  47. #include <netinet/in.h>
  48. #include <arpa/inet.h>
  49.  
  50. #if HAVE_FCNTL_H
  51. #include <fcntl.h>
  52. #else
  53. #if HAVE_SYS_FILE_H
  54. #include <sys/file.h>
  55. #endif
  56. #endif
  57.  
  58. #ifndef FD_CLOEXEC
  59. #define FD_CLOEXEC 1
  60. #endif
  61.  
  62. /* This code handles TCP connections.  It assumes a Berkeley socket
  63.    interface.  */
  64.  
  65. /* The normal "uucp" port number.  */
  66. #define IUUCP_PORT (540)
  67.  
  68. /* Local functions.  */
  69. static void utcp_free P((struct sconnection *qconn));
  70. static boolean ftcp_open P((struct sconnection *qconn, long ibaud,
  71.                 boolean fwait));
  72. static boolean ftcp_close P((struct sconnection *qconn,
  73.                  pointer puuconf,
  74.                  struct uuconf_dialer *qdialer,
  75.                  boolean fsuccess));
  76. static boolean ftcp_dial P((struct sconnection *qconn, pointer puuconf,
  77.                 const struct uuconf_system *qsys,
  78.                 const char *zphone,
  79.                 struct uuconf_dialer *qdialer,
  80.                 enum tdialerfound *ptdialer));
  81. static int itcp_port_number P((const char *zport));
  82.  
  83. /* The command table for a TCP connection.  */
  84. static const struct sconncmds stcpcmds =
  85. {
  86.   utcp_free,
  87.   NULL, /* pflock */
  88.   NULL, /* pfunlock */
  89.   ftcp_open,
  90.   ftcp_close,
  91.   ftcp_dial,
  92.   fsysdep_conn_read,
  93.   fsysdep_conn_write,
  94.   fsysdep_conn_io,
  95.   NULL, /* pfbreak */
  96.   NULL, /* pfset */
  97.   NULL, /* pfcarrier */
  98.   fsysdep_conn_chat,
  99.   NULL /* pibaud */
  100. };
  101.  
  102. /* Initialize a TCP connection.  */
  103.  
  104. boolean
  105. fsysdep_tcp_init (qconn)
  106.      struct sconnection *qconn;
  107. {
  108.   struct ssysdep_conn *q;
  109.  
  110.   q = (struct ssysdep_conn *) xmalloc (sizeof (struct ssysdep_conn));
  111.   q->o = -1;
  112.   q->ord = -1;
  113.   q->owr = -1;
  114.   q->zdevice = NULL;
  115.   q->iflags = -1;
  116.   q->iwr_flags = -1;
  117.   q->fterminal = FALSE;
  118.   q->ftli = FALSE;
  119.   q->ibaud = 0;
  120.  
  121.   qconn->psysdep = (pointer) q;
  122.   qconn->qcmds = &stcpcmds;
  123.   return TRUE;
  124. }
  125.  
  126. /* Free a TCP connection.  */
  127.  
  128. static void
  129. utcp_free (qconn)
  130.      struct sconnection *qconn;
  131. {
  132.   xfree (qconn->psysdep);
  133. }
  134.  
  135. /* Open a TCP connection.  If the fwait argument is TRUE, we are
  136.    running as a server.  Otherwise we are just trying to reach another
  137.    system.  */
  138.  
  139. static boolean
  140. ftcp_open (qconn, ibaud, fwait)
  141.      struct sconnection *qconn;
  142.      long ibaud;
  143.      boolean fwait;
  144. {
  145.   struct ssysdep_conn *qsysdep;
  146.   struct sockaddr_in s;
  147.   const char *zport;
  148.   uid_t ieuid;
  149.   boolean fswap;
  150.  
  151.   ulog_device ("TCP");
  152.  
  153.   qsysdep = (struct ssysdep_conn *) qconn->psysdep;
  154.  
  155.   qsysdep->o = socket (AF_INET, SOCK_STREAM, 0);
  156.   if (qsysdep->o < 0)
  157.     {
  158.       ulog (LOG_ERROR, "socket: %s", strerror (errno));
  159.       return FALSE;
  160.     }
  161.  
  162.   if (fcntl (qsysdep->o, F_SETFD,
  163.          fcntl (qsysdep->o, F_GETFD, 0) | FD_CLOEXEC) < 0)
  164.     {
  165.       ulog (LOG_ERROR, "fcntl (FD_CLOEXEC): %s", strerror (errno));
  166.       (void) close (qsysdep->o);
  167.       qsysdep->o = -1;
  168.       return FALSE;
  169.     }
  170.  
  171.   qsysdep->iflags = fcntl (qsysdep->o, F_GETFL, 0);
  172.   if (qsysdep->iflags < 0)
  173.     {
  174.       ulog (LOG_ERROR, "fcntl: %s", strerror (errno));
  175.       (void) close (qsysdep->o);
  176.       qsysdep->o = -1;
  177.       return FALSE;
  178.     }
  179.  
  180.   /* We save our process ID in the qconn structure.  This is checked
  181.      in ftcp_close.  */
  182.   qsysdep->ipid = getpid ();
  183.  
  184.   /* If we aren't waiting for a connection, we're done.  */
  185.   if (! fwait)
  186.     return TRUE;
  187.  
  188.   /* Run as a server and wait for a new connection.  The code in
  189.      uucico.c has already detached us from our controlling terminal.
  190.      From this point on if the server gets an error we exit; we only
  191.      return if we have received a connection.  It would be more robust
  192.      to respawn the server if it fails; someday.  */
  193.   bzero ((pointer) &s, sizeof s);
  194.   s.sin_family = AF_INET;
  195.   zport = qconn->qport->uuconf_u.uuconf_stcp.uuconf_zport;
  196.   s.sin_port = itcp_port_number (zport);
  197.   s.sin_addr.s_addr = htonl (INADDR_ANY);
  198.  
  199.   /* Swap to our real user ID when doing the bind call.  This will
  200.      permit the server to use privileged TCP ports when invoked by
  201.      root.  We only swap if our effective user ID is not root, so that
  202.      the program can also be made suid root in order to get privileged
  203.      ports when invoked by anybody.  */
  204.   fswap = geteuid () != 0;
  205.   if (fswap)
  206.     {
  207.       if (! fsuser_perms (&ieuid))
  208.     {
  209.       (void) close (qsysdep->o);
  210.       qsysdep->o = -1;
  211.       return FALSE;
  212.     }
  213.     }
  214.  
  215.   if (bind (qsysdep->o, (struct sockaddr *) &s, sizeof s) < 0)
  216.     {
  217.       if (fswap)
  218.     (void) fsuucp_perms ((long) ieuid);
  219.       ulog (LOG_FATAL, "bind: %s", strerror (errno));
  220.     }
  221.  
  222.   /* Now swap back to the uucp user ID.  */
  223.   if (fswap)
  224.     {
  225.       if (! fsuucp_perms ((long) ieuid))
  226.     ulog (LOG_FATAL, "Could not swap back to UUCP user permissions");
  227.     }
  228.  
  229.   if (listen (qsysdep->o, 5) < 0)
  230.     ulog (LOG_FATAL, "listen: %s", strerror (errno));
  231.  
  232.   while (! FGOT_SIGNAL ())
  233.     {
  234.       size_t clen;
  235.       int onew;
  236.       pid_t ipid;
  237.  
  238.       DEBUG_MESSAGE0 (DEBUG_PORT,
  239.               "ftcp_open: Waiting for connections");
  240.  
  241.       clen = sizeof s;
  242.       onew = accept (qsysdep->o, (struct sockaddr *) &s, &clen);
  243.       if (onew < 0)
  244.     ulog (LOG_FATAL, "accept: %s", strerror (errno));
  245.  
  246.       DEBUG_MESSAGE0 (DEBUG_PORT,
  247.               "ftcp_open: Got connection; forking");
  248.  
  249.       ipid = ixsfork ();
  250.       if (ipid < 0)
  251.     ulog (LOG_FATAL, "fork: %s", strerror (errno));
  252.       if (ipid == 0)
  253.     {
  254.       (void) close (qsysdep->o);
  255.       qsysdep->o = onew;
  256.  
  257.       /* Now we fork and let our parent die, so that we become
  258.          a child of init.  This lets the main server code wait
  259.          for its child and then continue without accumulating
  260.          zombie children.  */
  261.       ipid = ixsfork ();
  262.       if (ipid < 0)
  263.         {
  264.           ulog (LOG_ERROR, "fork: %s", strerror (errno));
  265.           _exit (EXIT_FAILURE);
  266.         }
  267.           
  268.       if (ipid != 0)
  269.         _exit (EXIT_SUCCESS);
  270.  
  271.       ulog_id (getpid ());
  272.  
  273.       return TRUE;
  274.     }
  275.  
  276.       (void) close (onew);
  277.  
  278.       /* Now wait for the child.  */
  279.       (void) ixswait ((unsigned long) ipid, (const char *) NULL);
  280.     }
  281.  
  282.   /* We got a signal.  */
  283.   usysdep_exit (FALSE);
  284.  
  285.   /* Avoid compiler warnings.  */
  286.   return FALSE;
  287. }
  288.  
  289. /* Close the port.  */
  290.  
  291. /*ARGSUSED*/
  292. static boolean
  293. ftcp_close (qconn, puuconf, qdialer, fsuccess)
  294.      struct sconnection *qconn;
  295.      pointer puuconf;
  296.      struct uuconf_dialer *qdialer;
  297.      boolean fsuccess;
  298. {
  299.   struct ssysdep_conn *qsysdep;
  300.   boolean fret;
  301.  
  302.   qsysdep = (struct ssysdep_conn *) qconn->psysdep;
  303.   fret = TRUE;
  304.   if (qsysdep->o >= 0 && close (qsysdep->o) < 0)
  305.     {
  306.       ulog (LOG_ERROR, "close: %s", strerror (errno));
  307.       fret = FALSE;
  308.     }
  309.   qsysdep->o = -1;
  310.  
  311.   /* If the current pid is not the one we used to open the port, then
  312.      we must have forked up above and we are now the child.  In this
  313.      case, we are being called from within the fendless loop in
  314.      uucico.c.  We return FALSE to force the loop to end and the child
  315.      to exit.  This should be handled in a cleaner fashion.  */
  316.   if (qsysdep->ipid != getpid ())
  317.     fret = FALSE;
  318.  
  319.   return fret;
  320. }
  321.  
  322. /* Dial out on a TCP port, so to speak: connect to a remote computer.  */
  323.  
  324. /*ARGSUSED*/
  325. static boolean
  326. ftcp_dial (qconn, puuconf, qsys, zphone, qdialer, ptdialer)
  327.      struct sconnection *qconn;
  328.      pointer puuconf;
  329.      const struct uuconf_system *qsys;
  330.      const char *zphone;
  331.      struct uuconf_dialer *qdialer;
  332.      enum tdialerfound *ptdialer;
  333. {
  334.   struct ssysdep_conn *qsysdep;
  335.   const char *zhost;
  336.   struct hostent *q;
  337.   struct sockaddr_in s;
  338.   const char *zport;
  339.   char **pzdialer;
  340.  
  341.   qsysdep = (struct ssysdep_conn *) qconn->psysdep;
  342.  
  343.   *ptdialer = DIALERFOUND_FALSE;
  344.  
  345.   zhost = zphone;
  346.   if (zhost == NULL)
  347.     {
  348.       if (qsys == NULL)
  349.     {
  350.       ulog (LOG_ERROR, "No address for TCP connection");
  351.       return FALSE;
  352.     }
  353.       zhost = qsys->uuconf_zname;
  354.     }
  355.  
  356.   errno = 0;
  357.   q = gethostbyname ((char *) zhost);
  358.   if (q != NULL)
  359.     {
  360.       s.sin_family = q->h_addrtype;
  361.       memcpy (&s.sin_addr.s_addr, q->h_addr, (size_t) q->h_length);
  362.     }
  363.   else
  364.     {
  365.       if (errno != 0)
  366.     {
  367.       ulog (LOG_ERROR, "gethostbyname (%s): %s", zhost, strerror (errno));
  368.       return FALSE;
  369.     }
  370.  
  371.       s.sin_family = AF_INET;
  372.       s.sin_addr.s_addr = inet_addr ((char *) zhost);
  373.       if ((long) s.sin_addr.s_addr == (long) -1)
  374.     {
  375.       ulog (LOG_ERROR, "%s: unknown host name", zhost);
  376.       return FALSE;
  377.     }
  378.     }
  379.  
  380.   zport = qconn->qport->uuconf_u.uuconf_stcp.uuconf_zport;
  381.   s.sin_port = itcp_port_number (zport);
  382.  
  383.   if (connect (qsysdep->o, (struct sockaddr *) &s, sizeof s) < 0)
  384.     {
  385.       ulog (LOG_ERROR, "connect: %s", strerror (errno));
  386.       return FALSE;
  387.     }
  388.  
  389.   /* Handle the dialer sequence, if any.  */
  390.   pzdialer = qconn->qport->uuconf_u.uuconf_stcp.uuconf_pzdialer;
  391.   if (pzdialer != NULL && *pzdialer != NULL)
  392.     {
  393.       if (! fconn_dial_sequence (qconn, puuconf, pzdialer, qsys, zphone,
  394.                  qdialer, ptdialer))
  395.     return FALSE;
  396.     }
  397.  
  398.   return TRUE;
  399. }
  400.  
  401. /* Get the port number given a name.  The argument will almost always
  402.    be "uucp" so we cache that value.  The return value is always in
  403.    network byte order.  This returns -1 on error.  */
  404.  
  405. static int
  406. itcp_port_number (zname)
  407.      const char *zname;
  408. {
  409.   boolean fuucp;
  410.   static int iuucp;
  411.   int i;
  412.   char *zend;
  413.   struct servent *q;
  414.  
  415.   fuucp = strcmp (zname, "uucp") == 0;
  416.   if (fuucp && iuucp != 0)
  417.     return iuucp;
  418.  
  419.   /* Try it as a number first.  */
  420.   i = strtol ((char *) zname, &zend, 10);
  421.   if (i != 0 && *zend == '\0')
  422.     return htons (i);
  423.  
  424.   q = getservbyname ((char *) zname, (char *) "tcp");
  425.   if (q == NULL)
  426.     {
  427.       /* We know that the "uucp" service should be 540, even if isn't
  428.      in /etc/services.  */
  429.       if (fuucp)
  430.     {
  431.       iuucp = htons (IUUCP_PORT);
  432.       return iuucp;
  433.     }
  434.       ulog (LOG_ERROR, "getservbyname (%s): %s", zname, strerror (errno));
  435.       return -1;
  436.     }
  437.  
  438.   if (fuucp)
  439.     iuucp = q->s_port;
  440.  
  441.   return q->s_port;
  442. }
  443.  
  444. #endif /* HAVE_TCP */
  445.